# coding:utf-8
from __future__ import unicode_literals
from sqlalchemy.exc import *

class mysql_utils(object):
	def __init__(self, db):
		self.db = db

	def db_insert(self, db_cls, **kwargs):
		print kwargs
		db_instance = db_cls(**kwargs)
		try:
			# 将用户添加到数据库会话中
			self.db.session.add(db_instance)

			# 将数据库会话中的变动提交到数据库中,如果不Commit,数据库中是没有改动的
			self.db.session.commit()
			return db_instance
		except IntegrityError:
			self.rollback()
			return "唯一索引冲突"
		except Exception as e:
			self.rollback()
			return str(e)

	def rollback(self):
		self.db.session.rollback()